package com.smile.service.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smile.mapper.AirportInfoMapper;
import com.smile.model.domain.FeisuanAirportInfo;
//import com.smile.service.handler.AbstractServicesHandler;
import com.smile.service.service.MqttMessageSender;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class DefaultServicesHandler extends AbstractServicesHandler {

    @Autowired
    private AirportInfoMapper airportInfoMapper;

    @Autowired
    private MqttMessageSender mqttMessageSender;

//    @Override
//    public Boolean process(String data) throws MqttException {
//        MqttMessage message = new MqttMessage();
//        message.setQos(0);
//        message.setRetained(true);
//        message.setPayload(data.getBytes());
////        MqttClient mqttClient = new MqttClient("tcp://120.211.119.131:1883", "feisuan-mqtt-producer-client - " + "8888", new MemoryPersistence());
//        MqttClient mqttClient = new MqttClient("tcp://120.211.119.131:1883", "services-" + "8UUXN2800A00HX", new MemoryPersistence());
//        MqttConnectOptions connOpts = new MqttConnectOptions();
//        connOpts.setUserName("feisuan");
//        connOpts.setPassword("feisuan123456".toCharArray());
//        mqttClient.connect(connOpts);
//
//        //获取所有在线的机场
//        QueryWrapper<FeisuanAirportInfo> queryWrapper = new QueryWrapper<>();
//        long timestamp = System.currentTimeMillis()-5000;
//        Date date = new Date(timestamp);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String formattedDate = sdf.format(date);
//        queryWrapper.gt("modify_time", formattedDate);
//        //维护在线的心跳
//        List<FeisuanAirportInfo> list = airportInfoMapper.selectList(queryWrapper);
//        for (FeisuanAirportInfo feisuanAirportInfo : list) {
//            mqttClient.publish("thing/product/" + feisuanAirportInfo.getGatewaySn() + "/services", message);
//            mqttClient.publish("thing/product/" + feisuanAirportInfo.getGatewaySn() + "/drc/down", message);
//        }
//        return Boolean.TRUE;
//    }

    @Override
    public Boolean process(String data, String gateway) throws MqttException {
//        MqttMessage message = new MqttMessage();
//        message.setQos(0);
//        message.setRetained(true);
//        message.setPayload(data.getBytes());
//        MqttClient mqttClient = new MqttClient("tcp://120.211.119.131:1883", "feisuan-mqtt-producer-client - " + "8888", new MemoryPersistence());
//        MqttClient mqttClient = new MqttClient("tcp://120.211.119.131:1883", "drc1-" + "8UUXN2800A00HX", new MemoryPersistence());
//        MqttConnectOptions connOpts = new MqttConnectOptions();
//        connOpts.setUserName("feisuan");
//        connOpts.setPassword("feisuan123456".toCharArray());
//        mqttClient.connect(connOpts);
        mqttMessageSender.sendMsg("thing/product/" + gateway + "/services",data);
//        mqttClient.publish("thing/product/" + gateway + "/services", message);

        return Boolean.TRUE;
    }

    @Override
    public Boolean processDrc(String data, String gateway) throws MqttException {
//        MqttMessage message = new MqttMessage();
//        message.setQos(0);
//        message.setRetained(true);
//        message.setPayload(data.getBytes());
////        MqttClient mqttClient = new MqttClient("tcp://120.211.119.131:1883", "feisuan-mqtt-producer-client - " + "8888", new MemoryPersistence());
//        MqttClient mqttClient = new MqttClient("tcp://120.211.119.131:1883", "drc1-" + "8UUXN2800A00HX", new MemoryPersistence());
//        MqttConnectOptions connOpts = new MqttConnectOptions();
//        connOpts.setUserName("feisuan");
//        connOpts.setPassword("feisuan123456".toCharArray());
//        mqttClient.connect(connOpts);
//        System.out.println(message);
//        System.out.println("thing/product/" + gateway + "/drc/down");
////        for (int i =0;i<7;i++){
//            mqttClient.publish("thing/product/" + gateway + "/drc/down", message);
////        }
        mqttMessageSender.sendMsg("thing/product/" + gateway + "/drc/down",data);
        return Boolean.TRUE;
    }
}
